﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.AppSettings;

namespace Pozr
{
    public class SettingsWrapper
    {
        private SettingsFile m_settingsFile;

        public SettingsWrapper(SettingsFile _settingsFile)
        {
            Middleware.WriteLogToFile("SettingsWrapper begin.");
            this.m_settingsFile = _settingsFile;
            Middleware.WriteLogToFile("SettingsWrapper end.");
        }

        public bool InitSettingsGroup(string app_id)
        {
            lock (m_settingsFile)
            {
                try
                {
                    if (!SettingsGroupAvailable(app_id))
                    {
                        m_settingsFile.Groups.Add(app_id);
                        m_settingsFile.Save();
                    }
                    return true;
                }
                catch (Exception e)
                {
                    Middleware.WriteLogToFile(e.Message);
                    return false;
                }
            }
        }

        public bool DeleteSettingsGroup(string app_id)
        {
            lock (m_settingsFile)
            {
                try
                {
                    if (SettingsGroupAvailable(app_id))
                    {
                        m_settingsFile.Groups.Remove(app_id);
                        m_settingsFile.Save();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception e)
                {
                    Middleware.WriteLogToFile(e.Message);
                    return false;
                }
            }
        }

        public bool SettingsGroupAvailable(string app_id)
        {
            try
            {
                if (m_settingsFile.Groups[app_id].Name != String.Empty)
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool SetSetting(string app_id, string setting_name, object data)
        {
            lock (m_settingsFile)
            {
                try
                {
                    if (m_settingsFile.Groups[app_id].Settings[setting_name].Value != null)
                    {
                        m_settingsFile.Groups[app_id].Settings[setting_name].Value = data;
                    }
                    m_settingsFile.Save();
                    return true;
                }
                catch (Exception)
                {
                    try
                    {
                        m_settingsFile.Groups[app_id].Settings.Add(new Setting(setting_name, data));
                        m_settingsFile.Save();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
            }

        }

        public bool AddSetting(string app_id, string setting_name, object data)
        {
            try
            {
                lock (m_settingsFile)
                {
                    m_settingsFile.Groups[app_id].Settings.Add(new Setting(setting_name, data));
                    m_settingsFile.Save();
                    return true;
                }
            }
            catch (Exception e)
            {
                Middleware.WriteLogToFile(e.Message);
                return false;
            }
        }

        public bool ModifySetting(string app_id, string setting_name, object data)
        {
            try
            {
                lock (m_settingsFile)
                {
                    m_settingsFile.Groups[app_id].Settings[setting_name].Value = data;
                    m_settingsFile.Save();
                    return true;
                }
            }
            catch (Exception e)
            {
                Middleware.WriteLogToFile(e.Message);
                return false;
            }
        }

        public bool DeleteSetting(string app_id, string setting_name)
        {
            try
            {
                lock (m_settingsFile)
                {
                    m_settingsFile.Groups[app_id].Settings.Remove(setting_name);
                    m_settingsFile.Save();
                    return true;
                }
            }
            catch (Exception e)
            {
                Middleware.WriteLogToFile(e.Message);
                return false;
            }

        }

        public List<String> GetSettingGroups()
        {
            List<String> groups = new List<String>();
            try
            {
                lock (m_settingsFile)
                {
                    foreach (SettingGroup g in m_settingsFile.Groups)
                    {
                        groups.Add(g.Name);
                    }
                }
                return groups;
            }
            catch (Exception e)
            {
                Middleware.WriteLogToFile(e.Message);
                return null;
            }
        }
        
        public Dictionary<String, object> GetSettings(String groupName)
        {
            Dictionary<String, object> settings = new Dictionary<String, object>();
            try
            {
                lock (m_settingsFile)
                {
                    foreach (Setting s in m_settingsFile.Groups[groupName].Settings)
                    {
                        settings.Add(s.Name, s.Value);
                    }
                }
                return settings;
            }
            catch (Exception e)
            {
                Middleware.WriteLogToFile(e.Message);
                return null;
            }
        }

        public object GetSetting(string app_id, string setting_name)
        {
            try
            {
                lock (m_settingsFile)
                {
                    if (m_settingsFile.Groups[app_id].Settings[setting_name].Value != null)
                    {
                        return m_settingsFile.Groups[app_id].Settings[setting_name].Value;
                    }
                    return null;
                }
            }
            catch (Exception e)
            {
                Middleware.WriteLogToFile(e.Message);
                return null;
            }
        }
    }
}
